package com.unknownman.block.controller.admin;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lianziyou.chain.common.api.CommonPage;
import com.lianziyou.chain.common.api.CommonParam;
import com.lianziyou.chain.common.api.CommonResult;
import com.unknownman.block.entity.TContractConfig;
import com.unknownman.block.service.TContractConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.cloud.annotation.SystemResource;
import org.cloud.constant.CoreConstant;
import org.cloud.core.redis.RedisUtil;
import org.cloud.exception.BusinessException;
import org.cloud.utils.DataDimensionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@Api(description = "ContractController", tags = "ContractController")
@Validated
@RestController
@RequestMapping("/admin/contract")
@SystemResource(path = "/admin/contract")
@Slf4j
public class ContractController {

    @Autowired
    private TContractConfigService contractConfigService;

    @Autowired
    RedisUtil redisUtil;

    @Value("${system.DataDimension.open:false}")
    private Boolean isSystemDataDimensionOpen;


    @ApiOperation("获取合约配置")
    @SystemResource(value = "/admin-getContractList", description = "获取合约配置", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION)
    @RequestMapping(method = RequestMethod.POST, value = "/getContractList")
    public CommonResult<CommonPage<TContractConfig>> getContractList(CommonParam param, String contractAddress, Integer contractType)
        throws BusinessException {

        QueryWrapper<TContractConfig> where = new QueryWrapper<>();
        where.eq(!StringUtils.isEmpty(contractAddress), "contract_address", contractAddress);
        where.eq(!ObjectUtils.isEmpty(contractType), "contract_type", contractType);
        if (isSystemDataDimensionOpen) {
            Set<String> contractAddressDataDimension = DataDimensionUtil.single().getCurrentUserDataDimensionByName("contract_address");
            if (!contractAddressDataDimension.contains("***")) {
                where.in("contract_address", contractAddressDataDimension);
            }
        }
        return CommonResult.success(contractConfigService.findListByPage(param.getPage(), param.getLimit(), "create_date desc", where));
    }

    @ApiOperation("编辑合约配置")
    @SystemResource(value = "/admin-updateContract", description = "编辑合约配置", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION)
    @RequestMapping(method = RequestMethod.POST, value = "/updateContract")
    public CommonResult updateContract(@RequestBody TContractConfig config) throws BusinessException {
        try {
            return contractConfigService.updateContract(config);
        } catch (Exception e) {
            log.error("{}", e);
            throw new BusinessException(e.getMessage());
        }
    }

    @ApiOperation("添加合约配置")
    @SystemResource(value = "/admin-addContract", description = "添加合约配置", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION)
    @RequestMapping(method = RequestMethod.POST, value = "/addContract")
    public CommonResult addContract(@RequestBody TContractConfig config) throws BusinessException {
        try {
            return contractConfigService.addContract(config);
        } catch (Exception e) {
            log.error("{}", e);
            throw new BusinessException(e.getMessage());
        }
    }

    @ApiOperation("启用禁用")
    @SystemResource(value = "/admin-updateStatus", description = "启用禁用", authMethod = CoreConstant.AuthMethod.BYUSERPERMISSION)
    @RequestMapping(method = RequestMethod.POST, value = "/updateStatus")
    public CommonResult updateStatus(Long contractConfigId, Integer status) throws BusinessException {
        try {
            return contractConfigService.updateStatus(contractConfigId, status);
        } catch (Exception e) {
            log.error("{}", e);
            throw new BusinessException(e.getMessage());
        }
    }


}
